<template>
  <div class="cart">
    <SimpleHeader title="购物车" />
    <FixedPad />
    <div class="cart-main">
      <van-checkbox-group v-model="result" @change="groupChange">
        <van-swipe-cell v-for="item in cartListGetter" :key="item.cartItemId">
          <slot name="left">
            <div class="left-wrapper">
              <van-checkbox icon-size="16px" :name="item.cartItemId">
              </van-checkbox>
              <div class="left">
                <div class="img-wrapper">
                  <img :src="localIP + item.goodsCoverImg" alt="" />
                </div>
                <div class="detail">
                  <div class="title">{{ item.goodsName }}</div>
                  <div class="price">￥{{ item.sellingPrice }}</div>
                </div>
              </div>
            </div>
          </slot>
          <template v-slot:right>
            <van-button
              text="删除"
              type="danger"
              class="delete-button"
              @click="removeGoodsApi(item.cartItemId)"
            ></van-button>
          </template>
        </van-swipe-cell>
      </van-checkbox-group>
      <Empty v-if="cartListGetter.length === 0"></Empty>
    </div>
    <div class="submit">
      <div class="submit-left">
        <van-checkbox v-model="all" icon-size="16px" @click="selectAll"></van-checkbox>
        <div class="totalPrice">总计: ￥{{ totalPrice }}</div>
      </div>
      <van-button
        color="#1baeae"
        size="large"
        round
        style="height: 40px; width: 110px"
        :disabled="cartListGetter.length === 0"
        >提交</van-button
      >
    </div>
    <FixedPad height="100px" />
  </div>
</template>

<script>
import SimpleHeader from '@/components/SimpleHeader'
import FixedPad from '@/components/FixedPad'
import Empty from '@/views/Cart/Empty'
import { mapGetters, mapActions } from 'vuex'
import { localIP } from '@/utils'
export default {
  name: 'Cart',
  components: {
    SimpleHeader,
    FixedPad,
    Empty
  },
  computed: {
    ...mapGetters({
      cartListGetter: 'cart/cartListGetter',
    }),
    totalPrice() {
      const selected = this.result
      let total = 0
      this.cartListGetter.forEach(c => {
        if (selected.includes(c.cartItemId)) {
          total += c.sellingPrice
        }
      })
      return total
    },
  },
  data() {
    return {
      localIP,
      result: [],
      all: false,
    }
  },
  mounted() {
    if (this.cartListGetter.length === 0) {
      this.getCartListApi()
    }
  },
  methods: {
    ...mapActions({
      getCartListApi: 'cart/getCartListApi',
      removeGoodsApi: 'cart/removeGoodsApi'
    }),

    // 列表选中和取消的回调
    groupChange(arr) {
      this.all = arr.length === this.cartListGetter.length ? true : false
    },

    // 点击全选或取消的回调
    selectAll() {
      if (this.all) {
        this.result.length === this.cartListGetter.length ? null : this.result = this.cartListGetter.map(item => item.cartItemId)
      } else {
        this.result = []
      }
    }

  },
}
</script>

<style lang="less" scoped>
@import '../../common/style/mixin.less';
.cart {
  font-size: 12px;
  .cart-main {
    .left-wrapper {
      display: flex;
      align-items: center;
      padding: 0 10px 0 15px;
      .left {
        display: flex;
        height: 104px;
        padding: 8px 8px 8px 0;
        margin-left: 2px;
        .img-wrapper {
          height: 100%;
          padding: 5px 0;
          img {
            height: 100%;
          }
        }
        .detail {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding: 10px 0;
          .title {
            font-size: 14px;
          }
          .price {
            font-size: 12px;
            color: @primary;
          }
        }
      }
    }
    .delete-button {
      height: 100%;
    }
  }
  .submit {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    left: 0;
    width: 100%;
    bottom: 50px;
    padding: 0 15px;
    background-color: #fff;
    .submit-left {
      display: flex;
      align-items: center;
      .totalPrice {
        padding-left: 20px;
        font-size: 16px;
        color: @primary;
      }
    }
  }
}
</style>
